package daa.levelup.aCipher;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CipherFrame extends JFrame {

    CipherManager CiM = new CipherManager();
    Field intex;
    Field keytex;
    Field restext;
    String[] items;
    private CipherSelect cipherSelect;

    public CipherFrame() throws HeadlessException {
        setupFrame();
        fillFrame();
    }

    private class CipherSelect extends JComboBox {
        private CipherSelect(Object[] items) {
            super(items);
            JLabel label = new JLabel("Select Method:");
            add(label);
        }
    }


    private void setupFrame() {

        setTitle("Cipher Manager");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Toolkit tool = Toolkit.getDefaultToolkit();


        intex = new Field(4, 40);
        keytex = new Field(4, 40);
        restext = new Field(4, 40);

        items = new String[]{
                "Caesar cipher",
                "Vizener cipher",
        };
        cipherSelect = new CipherSelect(items);
        cipherSelect.setEditable(false);


        JPanel panel1 = new JPanel();


        panel1.setLayout(new GridBagLayout());
        addItem(panel1, new JLabel("Input Text:"), 0, 0, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
        addItem(panel1, new JLabel("Input key:"), 0, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
        addItem(panel1, new JLabel("Select cipher type:"), 2, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.LINE_END);
        addItem(panel1, new JLabel("Result:"), 0, 2, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);

        addItem(panel1, intex.getScrollPane(), 1, 0, 3, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        addItem(panel1, keytex.getScrollPane(), 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
        addItem(panel1, cipherSelect, 3, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
        addItem(panel1, restext.getScrollPane(), 1, 2, 3, 3, GridBagConstraints.CENTER, GridBagConstraints.BOTH);


        ButtonPanel buttonPanel = new ButtonPanel();


        add(buttonPanel, BorderLayout.SOUTH);

        add(panel1);

        Dimension d = tool.getScreenSize();
        int coordX = d.width / 2;
        int coordY = d.height / 2;
        int windowWidth = 600;
        int windowHeight = 450;
        coordX = (int) (coordX - windowWidth * 0.75);
        coordY = (int) (coordY - windowHeight * 0.75);
        setBounds(coordX, coordY, windowWidth, windowHeight);


    }

    private void fillFrame() {

    }


    class ButtonPanel extends JPanel {

        public ButtonPanel() {
            ButtonGroup group = new ButtonGroup();
            JButton encButton = new JButton("Encrypt");
            JButton decButton = new JButton("Decrypt");
            group.add(encButton);
            ChangeCipherAction changeCipherAction = new ChangeCipherAction();
            encButton.addActionListener(changeCipherAction);
            group.add(decButton);
            decButton.addActionListener(changeCipherAction);
            add(encButton);
            add(decButton);
        }
    }


    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align, int fill) {
        GridBagConstraints gc = new GridBagConstraints();

        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(1, 2, 2, 1);
        gc.anchor = align;
        gc.fill = fill;
        gc.insets = new Insets(10, 10, 10, 10);
        p.add(c, gc);


    }

    class ChangeCipherAction implements ActionListener {


        @Override
        public void actionPerformed(ActionEvent e) {

            String s = (String) cipherSelect.getSelectedItem();
            if (s.equals("Caesar cipher")) {
                String res = CiM.EncryptAction("Caesar", keytex.getText(), intex.getText());
                restext.setText(res);
                restext.selectAll();
                String res1 = CiM.DecryptAction("Caesar", intex.getText(), intex.getText());
                restext.setText(res1);
                restext.selectAll();


            } else if (s.equals("Vizener cipher")) {
                String res = CiM.EncryptAction("Vizener", keytex.getText(), intex.getText());
                String res1 = CiM.DecryptAction("Vizener", intex.getText(), intex.getText());
            }
        }
    }

}
